const { STRING } = require('sequelize')
const { db } = require('../core/sequelize')
const { Sequelize, Model, INTEGER } = require('sequelize')

const { CategoryPexels } = require('./categoryPexels')
class Pexels extends Model {
    static async getPexelsList(pageIndex = 1, pageSize = 20, categoryid = '') {
        const whereObj = categoryid ? { where: { categoryid } } : ''
        // 查数量
        let count = await Pexels.count()
        let pexelsList = []
        if (count !== 0) {
            pexelsList = await Pexels.findAll(
                {
                    limit: pageSize,
                    offset: (pageIndex - 1) * pageSize,
                    // 关联查询
                    include: [{
                        // model: db.models.pexelsCategory,
                        association: Pexels.hasMany(CategoryPexels, {}),
                        attributes: ['id', 'pexel_id', 'categoryid'],
                        ...whereObj
                    }]

                }
            )
        }
        return { count, pageIndex, pageSize, pexelsList }
    }
}
Pexels.init(
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true, //设置主键
            autoIncrement: true,
        },
        author: Sequelize.STRING, //类型 字符串
        // authorLike: Sequelize.STRING, //类型 字符串
        authorLike: { type: STRING, field: 'authorLike' },
        avatar: Sequelize.STRING, //类型 字符串
        photo: Sequelize.STRING, //类型 字符串
        photoDownLink: { type: STRING, field: 'photoDownLink' },
        photoHeight: { type: STRING, field: 'photoHeight' },
        photoLink: { type: STRING, field: 'photoLink' },
        photoType: { type: STRING, field: 'photoType' },
        photoWidth: { type: STRING, field: 'photoWidth' },
        tags: Sequelize.STRING, //类型 字符串
    },
    {
        // underscored: true,
        sequelize: db,
        modelName: 'pexels', timestamps: false
    }
)
// Pexels.associate = function () {
//     Pexels.belongsToMany(db.models.category, {
//         through: Category,
//         foreignKey: 'pexelsid',
//         otherKey: 'categoryid',
//     })
// }

Pexels.associate = function () {


}
module.exports = { Pexels }
